<?php
// +----------------------------------------------------------------------
// | 有客商城 [ 山西幺九网络科技有限公司 版权所有 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.yuuck.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 燕南天 <81020302@qq.com> 2018/7/15/015 9:26
// +----------------------------------------------------------------------


namespace app\admin\service;

use app\admin\model\Store;
use think\Exception;

class StoreService
{
    private $model;
    private $status = [
        '0' => '未审核',
        '1' => '正常',
        '-1' => '已冻结'
    ];

    public function __construct()
    {
        $this->model = new Store();
    }

    /**
     * 店铺分页查询
     * @param $page
     * @param $limit
     * @param $map
     * @param $order
     * @return array
     * @throws \think\exception\DbException
     */
    public function getList($page, $limit, $map, $order='id desc'){
        $data = $this->model
            ->where($map)
            ->field('id,store_name,store_no,linkman,mobile,tel,stores_img,license_img,follow,status,uid,province,city,district,latitude,longitude,add_time,approve_time,upd_time,is_rec')
            ->order($order)
            ->paginate(array('list_rows'=>$limit,'page'=>$page))
            ->toArray();

        foreach ($data['data'] as $k=>$v){
            $data['data'][$k]['status_name'] = $this->status[$v['status']];
        }
        return $data;
    }

    /**
     * 申请店铺
     * @param $data
     * @return array|bool
     * @throws \think\exception\PDOException
     */
    public function add($data)
    {
        try{
            $this->model->startTrans();
            $res = $this->model->isUpdate(false)->save($data);
            $this->model->commit();
            return $res;
        }catch (Exception $e){
            $this->model->rollback();
//            throw new \Exception("商户添加失败");
            return ['code'=>'1008','msg'=>'商户添加失败','data'];
        }
    }

    /**
     * 修改
     * @param $data
     * @param $map
     * @return array|bool
     * @throws \think\exception\PDOException
     */
    public function upd($data, $map)
    {
        try{
            $this->model->startTrans();
            $res = $this->model->isUpdate(true)->save($data, $map);
            $this->model->commit();
            return $res;
        }catch (Exception $e){
            $this->model->rollback();
//            throw new \Exception("商户修改失败");
            return ['code'=>'1008','msg'=>'商户修改失败','data'];
        }
    }

}